package com.crm.dao.saling;

import com.crm.pojo.Dispatcher;
import com.crm.vo.DispacherVO;
import com.crm.vo.DispatcherContractClientUserInfoVO;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.CrudRepository;

import java.util.List;

/**
 * Created by dr5hx on 2018/8/10
 */
public interface DispatcherDao extends CrudRepository<Dispatcher, Long> {
    /**
     * 显示未删除的发货单
     *
     * @param disparcherId
     */
    @Query("update Dispatcher d set d.dispatcherIsDel='是' where d.dispatcherId=?1")
    @Modifying
    public void deleteDispatcherByDispatcherId(Long disparcherId);

    /**
     * 显示未删除的发货单列表
     *
     * @param pageable
     * @return
     */
    @Query("from Dispatcher  d where d.dispatcherIsDel='否'")
    public List<Dispatcher> showDipatcherList(Pageable pageable);

    //模糊查询发货单根据对应订单标题或发货单id
    @Query("select new com.crm.vo.DispacherVO(d,c.contractTopic) from Dispatcher d, Contract c where d.fkOrderId=c.contractId and d.dispatcherIsDel='否'and c.contractIsDel='否'and d.dispatcherState not like '%已发货%'  and c.contractTopic like ?1")
    public List<DispacherVO> getAllDispatchBymh(String q);

    //查询所有发货单
    @Query("select new com.crm.vo.DispacherVO(d,c.contractTopic) from Dispatcher d, Contract c where d.fkOrderId=c.contractId and d.dispatcherIsDel='否'and c.contractIsDel='否'and d.dispatcherState not like '%已发货%'")
    public List<DispacherVO> getAllDispatchBymh2();

    /**
     * 获取发货单详细信息
     *
     * @param pageable
     * @return
     */
    @Query("select new com.crm.vo.DispatcherContractClientUserInfoVO(dispatcher,contract,clientInfo,userInnfo) " +
            "from Dispatcher dispatcher,Contract contract, ClientInfo clientInfo,Userinfo userInnfo,Inandoutlog inAndOut " +
            "where dispatcher.fkOrderId=contract.contractId " +
            "and contract.fkCustomerId=clientInfo.cid and inAndOut.ioDispatherId=dispatcher.dispatcherId " +
            "and inAndOut.handmanId=userInnfo.uId and dispatcher.dispatcherIsDel='否' ")
    public List<DispatcherContractClientUserInfoVO> getDispatcherContractClientUserInfoVOList(Pageable pageable);

    /**
     * 获取发货单数量
     *
     * @return
     */
    @Query("select  count(d) from Dispatcher  d where d.dispatcherIsDel='否'")
    public Long getdispatcherListCount();

    @Query("select d from Dispatcher  d where d.dispatcherId=?1")
    public Dispatcher getDispatcherByDispatcherId(Long dipatcherId);
}
